package com.itawei.d2_UDP3;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.MulticastSocket;

public class ReceiveMessage {
    public static void main(String[] args) throws IOException {

        MulticastSocket ms = new MulticastSocket(10086);//10000端口接收数据
        //将本机地址添加到224.0.0.1的这一组中
        ms.joinGroup(InetAddress.getByName("224.0.0.1"));
        //2.接收数据
        //2.1创建一个包和字节数组
        byte[] bytes = new byte[1024];
        DatagramPacket dp = new DatagramPacket(bytes,bytes.length);
        while (true) {
            //3.解析包
            ms.receive(dp);
            //4.打印数据
            byte[] data = dp.getData();
            InetAddress address = dp.getAddress();//接收到发送端IP地址
            int port = dp.getPort();//接收到发送端端口号

            System.out.println("一个"+address.getHostName()+"的人，IP地址为"+address.getHostAddress()+"发送了数据:"+
                    new String(bytes,0,dp.getLength()));

        }
    }
}
